package com.caiyi.financial.nirvana.sms.controller;

import com.alibaba.fastjson.JSONObject;
import com.caiyi.financial.nirvana.sms.entity.SmsRole;
import com.caiyi.financial.nirvana.sms.enums.ReturnCodeEnum;
import com.caiyi.financial.nirvana.sms.querybean.PageBean;
import com.caiyi.financial.nirvana.sms.querybean.RoleQueryBean;
import com.caiyi.financial.nirvana.sms.service.SmsRoleService;
import com.youyu.common.api.Result;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mario on 2018/1/4 0004.
 * admin role管理接口
 */
@Api(description = "角色管理")
@RequestMapping("/sms/admin/role")
@RestController
public class RoleController {

    @Autowired
    private SmsRoleService roleService;

    @ApiOperation("查询全部角色")
    @PostMapping("/getRoles")
    public Result getRoles(@RequestBody RoleQueryBean bean) {
        PageHelper.startPage(bean.getPageNum(), bean.getPageSize());
        Example example = new Example(SmsRole.class);
        example.createCriteria()
                .andCondition("name like '%" + (bean.getName() == null ? "" : bean.getName()) + "%'")
                .andBetween("createTime", bean.getStartTime(), bean.getEndTime());
        example.setOrderByClause("id desc");
        Result result = new Result();
        result.setData(new PageBean<>(roleService.queryListByExample(example)));
        return result;
    }

    @ApiOperation("查询角色DIC")
    @PostMapping("/getRolesDic")
    public Result getRolesDic() {
        List<SmsRole> smsRoles = this.roleService.queryAll();
        List<JSONObject> roleArr = new ArrayList<>();
        for (SmsRole role : smsRoles) {
            JSONObject object = new JSONObject();
            object.put("code", role.getId());
            object.put("name", role.getName());
            roleArr.add(object);
        }
        Result result = new Result();
        result.setData(roleArr);
        return result;
    }

    @ApiOperation("添加角色")
    @PostMapping(value = "/addRole")
    public Result addRole(@RequestBody SmsRole role) {
        if (roleService.insertByEntitySelective(role) == 1) {
            return new Result();
        } else {
            Result result = new Result();
            result.setDesc(ReturnCodeEnum.FAILED.getDesc());
            result.setCode(ReturnCodeEnum.FAILED.getCode());
            return result;
        }
    }

    @ApiOperation("删除角色")
    @PostMapping(value = "/delRole")
    public Result delRole(@RequestParam Integer id) {
        SmsRole role = new SmsRole();
        role.setId(id);
        if (roleService.deleteByPrimaryKey(role) != 1) {
            Result result = new Result();
            result.setDesc(ReturnCodeEnum.FAILED.getDesc());
            result.setCode(ReturnCodeEnum.FAILED.getCode());
            return result;
        } else {
            return new Result();
        }
    }

    @ApiOperation("更新角色")
    @PostMapping(value = "/modRole")
    public Result modRole(@RequestBody SmsRole role) {
        if (roleService.updateByEntitySelective(role) != 1) {
            Result result = new Result();
            result.setDesc(ReturnCodeEnum.FAILED.getDesc());
            result.setCode(ReturnCodeEnum.FAILED.getCode());
            return result;
        } else {
            return new Result();
        }
    }

    @ApiOperation("分配权限")
    @PostMapping(value = "/modAuth")
    public Result modAuth(@RequestBody SmsRole role) {
        int res = roleService.updateByEntitySelective(role);
        if (res == 1) {
            return new Result();
        } else {
            Result result = new Result();
            result.setDesc(ReturnCodeEnum.FAILED.getDesc());
            result.setCode(ReturnCodeEnum.FAILED.getCode());
            return result;
        }
    }
}
